// Column chart
function type_0_chart(from_mask)
{
    var options_0 = {
        chart: {
            type: 'column',
             backgroundColor:'transparent'
        },
        title: {
            text: 'Active IP vs Inactive IP'
        },
        xAxis: {
            categories:[]
        },
        yAxis: {
            allowDecimals: false,
            min: 0,
            title: {
                text: 'Number of IPs'
            }
        },
        tooltip: {
            formatter: function() {
                return '<b>'+ this.x +'</b><br/>'+
                    this.series.name +': '+ this.y +'<br/>'+
                    'Total: '+ this.point.stackTotal;
            }
        },
        plotOptions: {
            column: {
                stacking: 'normal'
            }
        },
        series:[{},{}]
};

 $.getJSON("overview.php?type=0&from_mask="+from_mask,function(jdata) {
    var t = JSON.stringify( options_0 );
    var options = JSON.parse( t );
    var seriesArr = [];
    var series1;
    var series2;
    var d1;
    var d2;
    $.each(jdata[0], function(key, data) { if (key=="name"){d1=data;} if (key=="data"){d2=data;}})
    var d3;
    var series1={name:d1,data:d2};
    $.each(jdata[1], function(key, data) {if (key=="name"){d1=data;} if (key=="data"){d3=data;} })
    var categories=[];
    $.each(d3, function(key, data) {categories.push(data[0]);})
    //alert(categories);
    options.chart.renderTo = 'container3';
    options.chart.type = 'column';
    options.series[0].name="active";
    options.series[1].name="inactive";
    options.series[0].data=d2;
    options.series[1].data=d3;
    options.xAxis.categories=categories;
    var chart1 = new Highcharts.Chart(options);


    var options = JSON.parse( t );
    options.chart.renderTo = 'container2';
    options.chart.type = 'pie';
    options.series=[{}];
    options.series[0].name="Total";
    var d=[];
    var c=0
    for(var i in d2){
        //alert(d2[i][0]);
        c=c+parseInt(d2[i][1]);
    }
    d.push(["active",c]);
    var c=0
    for(var i in d3){
        //alert(d2[i][0]);
        c=c+parseInt(d3[i][1]);
    }
    d.push(["inactive",c]);
    options.series[0].data=d;
    options.xAxis.categories=categories;
    options.plotOptions={ pie: {
    }
   };
    options.tooltip={};
    options.title.text="Total IP";
    var chart2 = new Highcharts.Chart(options);
    chart2.setSize(600,300);


    var options = JSON.parse( t );
    options.chart.renderTo = 'container1';
    options.chart.type = 'pie';
    options.series=[{}];
    options.series[0].name="Total";
    var d=[];
    for(var i in d2){
        //alert(d2[i][0]);
        var t=[];
        t.push(d2[i][0]);
        t.push(parseInt(d2[i][1])+parseInt(d3[i][1]));
        d.push(t);
    }
    options.series[0].data=d;
    options.xAxis.categories=categories;
    options.tooltip={};
    options.title.text="No of IP in Region";
    var chart3 = new Highcharts.Chart(options);
    chart3.setSize(600,300);

})

}

function type_1_chart(from_mask)
{
    var options_0 = {
        chart: {
            type: 'column',
             backgroundColor:'transparent'
        },
        title: {
            text: 'Port Scan Result'
        },
        xAxis: {
            categories:[]
        },
        yAxis: {
            allowDecimals: false,
            min: 0,
            title: {
                text: 'Number of IPs'
            }
        },
        tooltip: {
            formatter: function() {
                return '<b>'+ this.x +'</b><br/>'+
                    this.series.name +': '+ this.y +'<br/>'+
                    'Total: '+ this.point.stackTotal+'<br/>'+
                    'Percentage: '+(this.point.y/this.point.stackTotal).toFixed(4)*100+'%';
            }
        },
        plotOptions: {
            column: {
                stacking: 'normal'
            }
        },
        series:[{},{}]
};

 $.getJSON("overview.php?type=1&from_mask="+from_mask,function(jdata) {

    var tmp_total=[];
    var tmp_region=[];
    //var tmp_total_com=[];
    //var tmp_region_com=[];
    var reg_key={};
    $.each(jdata, function(key, data) {
        var tuple=data;
        var port=tuple[0];
        var region=tuple[1];
        var count=tuple[2];
        if(tmp_total[port]==null)
        {
            tmp_total[port]=0;
            //tmp_total_com[port]=0;
        }
        if(tmp_region[port]==null)
        {
            tmp_region[port]=[];
            var t=[region,count];
            tmp_region[port].push(t);

        }
        else{
            var t=[region,count];
            tmp_region[port].push(t);
        }
        reg_key[region]=0;
        //alert(region+''+reg_key);
        tmp_total[port]=tmp_total[port]+parseInt(count);
    })
    //alert(reg_key);

    var categories=[];

    var data=[]
    for(var i in tmp_total){
        //alert(d2[i][0]);
    
        categories.push(i);
        var t=[i,tmp_total[i]];
        data.push(t);
    }
    //var options=options_0;
    var t = JSON.stringify( options_0 );
    var options = JSON.parse( t );
    options.chart.renderTo = 'container1';
    options.chart.type = 'pie';
    options.title.text="Port Scan Result";
    options.tooltip={
                pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
            };
    options.series=[{}];
    options.series[0].name="open port";
    options.series[0].data=data;
    options.xAxis.categories=categories;
    var chart1 = new Highcharts.Chart(options);
   

    var t = JSON.stringify( options_0 );
    var options = JSON.parse( t );
    var categories=[]
    options.series=[];
    var c=0;
    for(var i in reg_key){
        categories.push(i);
    }

    for(var i in tmp_region){
        //alert(d2[i][0]);
        options.series.push([]);
        options.series[c].name=i;
        options.series[c].data=tmp_region[i];
        c=c+1;
    }
    options.tooltip={
                formatter: function() {
                return '<b>'+ this.x +'</b><br/>'+
                    this.series.name +': '+ this.y +'<br/>'+
                    'Total: '+ this.point.stackTotal+'<br/>'+
                    'Percentage: '+(this.point.y/this.point.stackTotal).toFixed(4)*100+'%';
            }
            };
    options.chart.type = 'column';
options.title.text="Port Scan Result by Region";
    options.plotOptions.bar={
                stacking: 'normal'
            };
    options.chart.renderTo = 'container3';
    options.xAxis.categories=categories;
    var chart2 = new Highcharts.Chart(options);

})
}

function type_2_chart(from_mask)
{
    var options_0 = {
        chart: {
            type: 'column',
             backgroundColor:'transparent'
        },
        title: {
            text: 'Web Result'
        },
        xAxis: {
            categories:[]
        },
        yAxis: {
            allowDecimals: false,
            min: 0,
            title: {
                text: 'Number of IPs'
            }
        },
        tooltip: {
            formatter: function() {
                return '<b>'+ this.x +'</b><br/>'+
                    this.series.name +': '+ this.y +'<br/>'+
                    'Total: '+ this.point.stackTotal+'<br/>'+
                    'Percentage: '+(this.point.y/this.point.stackTotal).toFixed(4)*100+'%';
            }
        },
        plotOptions: {
            column: {
                stacking: 'normal'
            }
        },
        series:[{},{}]
};

 $.getJSON("overview.php?type=2&from_mask="+from_mask,function(jdata) {

    var tmp_total=[];
    var tmp_region=[];
    //var tmp_total_com=[];
    //var tmp_region_com=[];
    var reg_key={};
    $.each(jdata, function(key, data) {
        var tuple=data;
        var port=tuple[0];
        var region=tuple[1];
        var count=tuple[2];
        if(tmp_total[port]==null)
        {
            tmp_total[port]=0;
            //tmp_total_com[port]=0;
        }
        if(tmp_region[port]==null)
        {
            tmp_region[port]=[];
            var t=[region,count];
            tmp_region[port].push(t);

        }
        else{
            var t=[region,count];
            tmp_region[port].push(t);
        }
        reg_key[region]=0;
        //alert(region+''+reg_key);
        tmp_total[port]=tmp_total[port]+parseInt(count);
    })
    //alert(reg_key);

    var categories=[];

    var data=[]
    for(var i in tmp_total){
        //alert(d2[i][0]);
    
        categories.push(i);
        var t=[i,tmp_total[i]];
        data.push(t);
    }
    //var options=options_0;
    var t = JSON.stringify( options_0 );
    var options = JSON.parse( t );
    options.chart.renderTo = 'container1';
    options.chart.type = 'pie';
    options.title.text="Web Result";
    options.tooltip={
                pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b><b>{point.y}</b>'
            };
    options.series=[{}];
    options.series[0].name="%";
    options.series[0].data=data;
    options.series[0].point={events:{
            click: function(e) {
                //alert(this.name+from_mask);
                type_detail_chart(from_mask,this.name);

           }
        }
    }
    options.xAxis.categories=categories;
    var chart1 = new Highcharts.Chart(options);
   

    var t = JSON.stringify( options_0 );
    var options = JSON.parse( t );
    var categories=[]
    options.series=[];
    var c=0;
    for(var i in reg_key){
        categories.push(i);
    }

    for(var i in tmp_region){
        //alert(d2[i][0]);
        options.series.push([]);
        options.series[c].name=i;
        options.series[c].data=tmp_region[i];
        c=c+1;
    }
    options.tooltip={
                formatter: function() {
                return '<b>'+ this.x +'</b><br/>'+
                    this.series.name +': '+ this.y +'<br/>'+
                    'Total: '+ this.point.stackTotal+'<br/>'+
                    'Percentage: '+(this.point.y/this.point.stackTotal).toFixed(4)*100+'%';
            }
            };
    options.chart.type = 'column';
options.title.text="Web Status Result by Region";
    options.plotOptions.bar={
                stacking: 'normal'
            };
    options.chart.renderTo = 'container3';
    options.xAxis.categories=categories;
    var chart2 = new Highcharts.Chart(options);

})
}

function type_detail_chart(from_mask,detailtype)
{
    var options_0 = {
        chart: {
            type: 'column',
             backgroundColor:'transparent'
        },
        title: {
            text: 'Web Result'
        },
        xAxis: {
            categories:[]
        },
        yAxis: {
            allowDecimals: false,
            min: 0,
            title: {
                text: 'Number of IPs'
            }
        },
        tooltip: {
            formatter: function() {
                return '<b>'+ this.x +'</b><br/>'+
                    this.series.name +': '+ this.y +'<br/>'+
                    'Total: '+ this.point.stackTotal+'<br/>'+
                    'Percentage: '+(this.point.y/this.point.stackTotal).toFixed(4)*100+'%';
            }
        },
        plotOptions: {
            column: {
                stacking: 'normal'
            }
        },
        series:[{},{}]
};

 $.getJSON("overview.php?type=3&from_mask="+from_mask+"&detail="+detailtype,function(jdata) {

    var tmp_total=[];
    var tmp_region=[];
    //var tmp_total_com=[];
    //var tmp_region_com=[];
    var reg_key={};
    $.each(jdata, function(key, data) {
        var tuple=data;
        var port=tuple[0];
        var region=tuple[1];
        var count=tuple[2];
        if(tmp_total[port]==null)
        {
            tmp_total[port]=0;
            //tmp_total_com[port]=0;
        }
        if(tmp_region[port]==null)
        {
            tmp_region[port]=[];
            var t=[region,count];
            tmp_region[port].push(t);

        }
        else{
            var t=[region,count];
            tmp_region[port].push(t);
        }
        reg_key[region]=0;
        //alert(region+''+reg_key);
        tmp_total[port]=tmp_total[port]+parseInt(count);
    })
    //alert(reg_key);

    var categories=[];

    var data=[]
    for(var i in tmp_total){
        //alert(d2[i][0]);
    
        categories.push(i);
        var t=[i,tmp_total[i]];
        data.push(t);
    }
    //var options=options_0;
    var t = JSON.stringify( options_0 );
    var options = JSON.parse( t );
    options.chart.renderTo = 'container2';
    options.chart.type = 'pie';
    options.title.text="Web Result";
    options.tooltip={
                pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b><b>{point.y}</b>'
            };
    options.series=[{}];
    options.series[0].name="%";
    options.series[0].data=data;
    options.xAxis.categories=categories;
    var chart1 = new Highcharts.Chart(options);
    chart1.setSize(600,300);

})
}

function simpleSearch()
{
 
    $( "#from1" ).datepicker( "option", "dateFormat", "yy-mm-dd");
    var from_mask = jQuery("#from1").val();
    var type_mask = jQuery("#type1").val();
    if(type_mask==0){
        type_0_chart(from_mask);
    }
    if(type_mask==1){
        type_1_chart(from_mask);
    }
    if(type_mask==2){
        type_2_chart(from_mask);
    }
}
